source ./flow/pgr/pg_psw_conf.tcl

proc pr_add_ENDCAP {} {
    dbDeleteObj [dbget top.insts.name  *xoendcap* -p] ;# delete the manual copy tap cell
    deleteFiller -prefix $PG(ENDCAP_prefix)
    deleteFiller -prefix VSTACK_

    ### Prevention insertion for max vertical stack length ###
    setFillerMode -reset
    setFillerMode -vertical_stack_max_length 29.52
    setFillerMode -corePrefix $PG(ENDCAP_prefix)
    setFillerMode -vertical_stack_left_edge_cell $PG(ENDCAP_R_REPLACE)
    setFillerMode -vertical_stack_right_edge_cell $PG(ENDCAP_L_REPLACE)
    setFillerMode -vertical_stack_left_edge_exception_cell $PG(ENDCAP_R_Cell)
    setFillerMode -vertical_stack_right_edge_exception_cell $PG(ENDCAP_L_Cell) 

    setEndCapMode -reset
    setEndCapMode -fitGap true
    setEndCapMode -min_jog_width  [expr int(($PG(ENDCAP_R_Width) + $PG(ENDCAP_L_Width) + $PG(core_site) + $PG(uscale) )/$PG(core_site))]
    setEndCapMode -min_jog_height 3
    setEndCapMode -min_vertical_channel_width   [expr int(($PG(ENDCAP_R_Width) + $PG(ENDCAP_L_Width) + $PSW(global_psw_cell_width) + $PG(uscale) )/$PG(core_site))]
    setEndCapMode -min_horizontal_channel_width 3

    set pd_list [dbget -u -e [dbget -p top.pds.group {.area > 0} -p].name]

    if { [llength $pd_list] > 0 } {
    foreach pd_name $pd_list {  

        if { [lsearch $PG(domain_psw) $pd_name ] < 0 } { 
        #workaround the for boundary tap insertion
        setEndCapMode -boundary_tap true
        set_well_tap_mode -reset
        set_well_tap_mode -top_tap_cell [lindex $PG(ENDCAP_T_Cell) 0] -bottom_tap_cell [lindex $PG(ENDCAP_B_Cell) 0]
        set_well_tap_mode -rule $PG(pgPitch)
        setEndCapMode 								\
                        -prefix            $PG(ENDCAP_prefix)			\
                        -topEdge           $PG(ENDCAP_T_Cell) 		  	\
                        -bottomEdge        $PG(ENDCAP_B_Cell) 			\
                        -leftEdge          $PG(ENDCAP_L_Cell) 			\
                        -leftTopCorner     $PG(ENDCAP_TL_OuterCorner) 		\
                        -leftBottomCorner  $PG(ENDCAP_BL_OuterCorner) 		\
                        -rightEdge         $PG(ENDCAP_R_Cell) 			\
                        -rightTopEdge      $PG(ENDCAP_TL_InnerCorner) 		\
                        -rightBottomEdge   $PG(ENDCAP_BR_InnerCorner)		\
                        -rightTopEdgeNeighbor $PG(ENDCAP_TR_Neighbor) 		\
                        -rightBottomEdgeNeighbor $PG(ENDCAP_BR_Neighbor)
                    # -leftTopEdge       $PG(ENDCAP_L_Cell) 			\
                    # -leftBottomEdge    $PG(BOUNDARY_NINCORNER) 			\
                    # -rightTopCorner    $PG(ENDCAP_TR_OuterCorner) 		\
                    # -rightBottomCorner $PG(ENDCAP_BR_OuterCorner) 		\

        } else {
        #workaround the for boundary tap insertion
        setEndCapMode -boundary_tap true
        set_well_tap_mode -reset
        set_well_tap_mode -top_tap_cell [lindex $PG(ENDCAP_T_Cell_Vpp) 0] -bottom_tap_cell [lindex $PG(ENDCAP_B_Cell_Vpp) 0]
        set_well_tap_mode -rule $PG(pgPitch)
        setEndCapMode 							\
                        -prefix            $PG(ENDCAP_prefix)			\
                        -topEdge           $PG(ENDCAP_T_Cell) 		  	\
                        -bottomEdge        $PG(ENDCAP_B_Cell) 			\
                        -leftEdge          $PG(ENDCAP_L_Cell) 			\
                        -leftTopCorner     $PG(ENDCAP_TL_OuterCorner) 		\
                        -leftBottomCorner  $PG(ENDCAP_BL_OuterCorner) 		\
                        -rightEdge         $PG(ENDCAP_R_Cell) 			\
                        -rightTopEdge      $PG(ENDCAP_TL_InnerCorner) 		\
                        -rightBottomEdge   $PG(ENDCAP_BR_InnerCorner)		\
                        -rightTopEdgeNeighbor $PG(ENDCAP_TR_Neighbor) 		\
                        -rightBottomEdgeNeighbor $PG(ENDCAP_BR_Neighbor)
        }
        #addFiller -prevent_vertical_stack_max_length_violation -powerDomain $pd_name 
        addEndCap -powerDomain $pd_name
    }

    } else {
    setEndCapMode 							\
                        -prefix            $PG(ENDCAP_prefix)			\
                        -topEdge           $PG(ENDCAP_T_Cell) 		  	\
                        -bottomEdge        $PG(ENDCAP_B_Cell) 			\
                        -leftEdge          $PG(ENDCAP_L_Cell) 			\
                        -leftTopCorner     $PG(ENDCAP_TL_OuterCorner) 		\
                        -leftBottomCorner  $PG(ENDCAP_BL_OuterCorner) 		\
                        -rightEdge         $PG(ENDCAP_R_Cell) 			\
                        -rightTopEdge      $PG(ENDCAP_TL_InnerCorner) 		\
                        -rightBottomEdge   $PG(ENDCAP_BR_InnerCorner)		\
                        -rightTopEdgeNeighbor $PG(ENDCAP_TR_Neighbor) 		\
                        -rightBottomEdgeNeighbor $PG(ENDCAP_BR_Neighbor)
        
    #addFiller -prevent_vertical_stack_max_length_violation 
    addEndCap
    }
}

# add tap cells
proc pr_add_TAPCELL {} {
    puts "==================="
    puts "===add tap cells==="
    puts "==================="
    #get_well_tap_mode
    set PG(WELLTAP_Cell) {TAPCELLBWP7D5T20P96CPD TAPCELLBWP7D5T20P96CPD}
    set tap_box [dbget top.fPlan.corebox]
    set tap_in_va [lindex $PG(WELLTAP_Cell) 0]
    #set Tapcellstart [expr [lindex [lindex [dbget top.fPlan.box] 0] 2]/4]
    set Tapcellstart [expr [lindex [lindex [dbget top.fPlan.box] 0] 2]/112]
    addWellTap -cell $tap_in_va -checkerBoard -cellInterval [expr $Tapcellstart*2+1] -inRowOffset [expr $Tapcellstart-15]
}

# add fill cells and DCAP cells
proc pr_add_FILLCELL {} {
    puts "======================"
    puts "====add fill cells===="
    puts "======================"
    dbset [dbget top.insts.cell.subClass cover -p2].pStatus unplaced
    setFillerMode -reset
    setFillerMode -corePrefix EDI_FILL -fitGap true
    setFillerMode -verticalStackMaxLength 200
    setFillerMode -verticalStackRepairCell FILL1BWP7D5T16P96CPD
    setFillerMode -verticalStackExceptionCell "$conf(libcell,welltap) FILL1BWP7D5T16P96CPD $conf(libcell,boundary)"
    #add dcap cell
    addFiller -cell [concat $conf(libcell,dcap) $conf(libcell,fill) $conf(libcell,lvtfill)] -prefix EDI_FILLMET -fitGap -fixVerticalStackMaxLengthViolation -doDRC true
    #addFiller -cell [concat $conf(libcell,fill) $conf(libcell,lvtfill)] -prefix EDI_FILLMET -fitGap -fixVerticalStackMaxLengthViolation -doDRC true
}